from chimera.core.remoteobject import RemoteObject
from chimera.util.coord import Coord

if __name__ == "__main__":
    print "This module is not intended to be run as a script."

class DbStar(RemoteObject):
    def __init__(self, entity):
        RemoteObject.__init__(self)
        
        self.objname = str(entity.objname)
        self.ra = float(entity.ra)
        self.dec = float(entity.dec)
        self.catalogs = []
        for catalog in entity.catalogs:
            self.catalogs.append(str(catalog.name))
        self.magnitudes = {}
        for magnitude in entity.magnitudes:
            self.magnitudes[str(magnitude.filter)] = float(magnitude.magnitude)
        self.exposures = []
        for exposure in entity.exposures:
            self.exposures.append(str(exposure.exposure.filename))

    def getObjName(self):
        return self.objname

    def getRA(self):
        return Coord.fromD(self.ra).toHMS()

    def getDec(self):
        return Coord.fromD(self.dec).toDMS()

    def getCatalogs(self):
        return self.catalogs

    def getMagnitudes(self):
        return self.magnitudes

    def getExposures(self):
        return self.exposures

    def __repr__(self):
        return '<dataproxy.Star "%s" (%s %s)>' % (self.objname, self.ra, self.dec)

class DbStarMagnitude(RemoteObject):
    def __init__(self, entity):
        RemoteObject.__init__(self)
        
        self.source = str(entity.source.name)
        self.filter = str(entity.filter)
        self.magnitude = float(entity.magnitude)

class DbExposure(RemoteObject):
    def __init__(self, entity, includestars = True):
        RemoteObject.__init__(self)
        
        self.filename = str(entity.filename)
        self.ra = float(entity.ra)
        self.dec = float(entity.dec)
        self.alt = float(entity.alt)
        self.az = float(entity.az)
        self.lst = float(entity.lst)
        self.latitude = float(entity.latitude)
        self.longitude = float(entity.longitude)
        self.obsdate = entity.obsdate
        self.imagetype = str(entity.imagetype)
        self.exptime = float(entity.exptime)
        self.filter = str(entity.filter)
        self.observer = str(entity.observer)
        self.stars = []
        if includestars:
            for star in entity.stars:
                self.stars.append(DbExposureStar(star, self))
        self.notes = []
        for note in entity.tasks:
            self.notes.append(DbExposureNote(note))

    def getFileName(self):
        return self.filename
    
    def getRA(self):
        return Coord.fromD(self.ra).toHMS()
    
    def getDec(self):
        return Coord.fromD(self.dec).toDMS()
    
    def getAlt(self):
        return Coord.fromD(self.alt).toDMS()
    
    def getAz(self):
        return Coord.fromD(self.az).toDMS()
    
    def getLST(self):
        return Coord.fromD(self.lst).toHMS()
    
    def getLatitude(self):
        return Coord.fromD(self.latitude).toDMS()
    
    def getLongitude(self):
        return Coord.fromD(self.longitude).toDMS()
    
    def getObsDate(self):
        return self.obsdate
    
    def getImageType(self):
        return self.imagetype
    
    def getExpTime(self):
        return self.exptime
    
    def getFilter(self):
        return self.filter
    
    def getObserver(self):
        return self.observer

    def getStars(self):
        expstars = []
        for expstar in self.stars:
            self.getDaemon().connect(expstar)
            expstars.append(expstar).getProxy()
        return expstars

    def getNotes(self):
        notes = []
        for note in self.notes:
            self.getDaemon().connect(note)
            notes.append(note).getProxy()
        return notes

    def __repr__(self):
        return '<dataproxy.Exposure "%s">' % (self.filename)

class DbExposureStar(RemoteObject):
    def __init__(self, entity, exposure = None):
        RemoteObject.__init__(self)

        if exposure == None:
            self.exposure = DbExposure(entity.exposure, includestars = False)
        else:
            self.exposure = exposure
        self.star = DbStar(entity.star)
        self.photra = float(entity.photra)
        self.photdec = float(entity.photdec)
        self.alt = float(entity.alt)
        self.az = float(entity.az)
        self.fwhm = float(entity.fwhm)
        self.flux = float(entity.flux)
        self.magnitude = float(entity.magnitude)
        self.airmass = float(entity.airmass)

    def getRA(self):
        return self.star.getRA()

    def getDec(self):
        return self.star.getDec()


class DbExposureNote(RemoteObject):
    name = ""
    detail = ""
    date = None

    def __init__(self, entity):
        RemoteObject.__init__(self)

        self.name = str(entity.name)
        self.detail = str(entity.detail)
        self.date = entity.date

    def __repr__(self):
        return '<dataproxy.ExposureNote "%s" (%s)>' % (self.name, self.date)